package com.dongdongshop.controller;

import com.dongdongshop.data.Page;
import com.dongdongshop.data.Result;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.pojo.TbTypeTemplate;
import com.dongdongshop.service.ItemCatService;
import com.dongdongshop.service.TypeTemplateService;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("ItemCat")
public class ItemCatController {

    @DubboReference
    private ItemCatService itemCatService;

    @DubboReference
    private TypeTemplateService templateService;

    @RequestMapping("show")
    public String show(Model model){
        List<TbTypeTemplate> t = templateService.show();
        model.addAttribute("t",t);
        return "admin/item_cat";
    }

    @RequestMapping("toShow")
    @ResponseBody
    public Result toShow(Long parentId,
                         @RequestParam(defaultValue = "1")Integer pageNum,
                         @RequestParam(defaultValue = "3")Integer pageSize){
        Page<TbItemCat> page = itemCatService.toShow(parentId,pageNum,pageSize);
        return Result.ok().setData(page);
    }

    @RequestMapping("add")
    @ResponseBody
    public Result add(TbItemCat tbItemCat){
        itemCatService.add(tbItemCat);
        return Result.ok();
    }

    @RequestMapping("toUpdate")
    @ResponseBody
    public Result toUpdate(Long id){
        TbItemCat tbItemCat = itemCatService.toUpdate(id);
        return Result.ok().setData(tbItemCat);
    }

    @RequestMapping("updateInfo")
    @ResponseBody
    public Result updateInfo(TbItemCat tbItemCat){
        itemCatService.updateInfo(tbItemCat);
        return Result.ok();
    }

    @RequestMapping("deleteInfo")
    @ResponseBody
    public Integer deleteInfo(Long[] ids){
        Integer i = itemCatService.deleteInfo(ids);
        return i;
    }
}
